package com.intexh.quickandroid.module.chat.util;

import android.os.AsyncTask;

import com.intexh.quickandroid.module.chat.sprinkles.Model;
import com.intexh.quickandroid.module.chat.sprinkles.ModelList;
import com.intexh.quickandroid.module.chat.sprinkles.Query;
import com.intexh.quickandroid.module.chat.sprinkles.QueryBuilder;
import com.intexh.quickandroid.module.chat.sprinkles.Transaction;
import com.intexh.quickandroid.module.chat.sprinkles.Utils;
import com.intexh.quickandroid.utils.ValidateUtils;

import java.util.List;
import java.util.Objects;


/**
 * 数据库工具类 : 
 * 
 * @author cp
 *
 */
public class DBUtil {
	
	public final String TAG="DBUtil"; //调试标签
	
	public static boolean saveSync(Model model){
		try {
			return model.save();
		} catch (Throwable e){
			return false;
		}	
	}


	public static  void saveAync(Model entity,Model.OnSavedCallback callback){
	  try{
		  entity.saveAsync(callback);
	  } catch (Throwable e){

	  }
	}

	
	/**
	 * 
	 * @param entityClass
	 * @param updateColumName
	 * @return
	 */
	public static long getLastUpdateTime(Class entityClass, String updateColumName){
		return 0;	
	}
	
		
	public static void deleteOneTableSync(Class entityClass){
		Query.one(entityClass, "delete from "+ Utils.getTableName(entityClass)).get();
	}



	/**
	 * @param callback
	 * @param tables
	 */
	public static void deletAllTableAsync(final Model.OnDeletedCallback callback, final Class[] tables){
        new AsyncTask<String, String, String>(){
			@Override
			protected String doInBackground(String... params) {
				Transaction transaction = new Transaction();
				try {
					for (Class table : tables) {
						Query.one(table, "delete from "+ Utils.getTableName(table)).get();
					}
					transaction.setSuccessful(true);					
				} catch (Throwable e){
					e.printStackTrace();
				}finally{
					transaction.finish();
				}								
				return "";			
			}
			
			@Override
			protected void onPostExecute(String result) {
              callback.onDeleted();
			}
        }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
	}
	
	
	public static void deleteAlltableSync(final Class[] tables){
		Transaction transaction = new Transaction();
		try {
			for (Class table : tables) {
				Query.one(table, "delete from "+ Utils.getTableName(table)).get();
			}
			transaction.setSuccessful(true);					
		} catch (Throwable e){
			e.printStackTrace();
		}finally{
			transaction.finish();
		}	
	}


	public static void getAllAsync(final Class entityClass, final String where, final String order, final int pageSize, final DataLoadListener listener){

		new AsyncTask<Objects,Integer,ModelList>(){
			@Override
			protected ModelList doInBackground(Objects... params){
				QueryBuilder b = QueryBuilder.create(entityClass);
				if (ValidateUtils.isValidate(where)) {
					b.where(where);
				}
				if (ValidateUtils.isValidate(order)) {
					b.order(order);
				}
				if (pageSize>0) {
					b.limit(pageSize);
				}
				List list = b.list();
				ModelList modleList = new ModelList<>();
				if (list!=null) {
					modleList.addAll(list);
				}
				return modleList;
			}

			@Override
			protected void onPostExecute(ModelList modelList){
				listener.onDataLoad(modelList);
			}
		}.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

	}
	
	public static ModelList getAll(Class entityClass, String where, String order, int pageSize){
		QueryBuilder b = QueryBuilder.create(entityClass);	      
	    if (ValidateUtils.isValidate(where)) {
	        b.where(where);
		}
	    if (ValidateUtils.isValidate(order)) {
	        b.order(order);
		}
	    if (pageSize>0) {
		    b.limit(pageSize);	
		}
		List list = b.list();
		
		ModelList modleList = new ModelList<Model>();		
        if (list!=null) {
    		modleList.addAll(list);
		}
		return modleList;
	}
	
	public static ModelList beansToEnties(List list){
		ModelList modleList = new ModelList<Model>();		
        if (list!=null) {
    		modleList.addAll(list);
		}
		return modleList;
	}
	
	
	
	public static ModelList getAll(Class entityClass){
		QueryBuilder b = QueryBuilder.create(entityClass);	      
		List list = b.list();
        ModelList modleList = new ModelList<Model>();
        if (list!=null) {
      		modleList.addAll(list);
  		}
		return modleList;	
	}
	
   public interface  DataLoadListener{
	  void onDataLoad(ModelList list);
   }
	
	
	
}
